package com.iwhalecloud.citybrain.flow.platform.manager.application.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.iwhalecloud.citybrain.flow.platform.manager.application.common.threadlocal.CommonOperateLogHolder;
import com.iwhalecloud.citybrain.flow.platform.manager.common.client.HttpCommandDto;
import com.iwhalecloud.citybrain.flow.platform.manager.common.client.HttpCommandResultDto;
import com.iwhalecloud.citybrain.flow.platform.manager.common.client.HttpProcessInstanceHandleRelDto;
import com.iwhalecloud.citybrain.flow.platform.manager.common.client.HttpProcessInstanceHandleRelInfoDto;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.form.FormProcessInstanceHandleRelVo;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.form.HttpProcessInstanceHandleRelVo;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.repository.FlowProcessInstanceHandleRelRepository;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
　　* @description: 流程实例中处理关系实现句柄
　　* @author shenrm
　　* @date 2021/10/20 11:31
　　*/
@Service
public class ProcessInstanceHandleRelCommandHandler extends AbstractCommandHandler {

    @Autowired
    private FlowProcessInstanceHandleRelRepository handleRelRepository;

    @Override
    public String handleType() {
        return PROCESS_INSTANCE_HANDLE_REL;
    }

    @Override
    public HttpCommandResultDto handle(String params) {
        HttpProcessInstanceHandleRelDto handleRelDto = JSONObject.parseObject(params, HttpProcessInstanceHandleRelDto.class);
        return handle(handleRelDto);
    }

    @Override
    public HttpCommandResultDto handle(HttpCommandDto commandDto) {
        HttpProcessInstanceHandleRelDto handleRelDto = (HttpProcessInstanceHandleRelDto) commandDto;
        CommonOperateLogHolder.getContent().setRequest(handleRelDto);

        // 阅办人为空
        if(StringUtils.isBlank(handleRelDto.getReadCopyTo())) {
            return resolveFailResult(null, "readCopyTo不能为空");
        }

        // 设置默认的分页参数
        super.processPageParams(handleRelDto);

        HttpCommandResultDto result = resolveResult(null, "查询成功");
        doHandle(handleRelDto, result);
        return result;
    }

    /**
     * 处理逻辑
     * @param handleRelDto
     * @param result
     */
    private void doHandle(HttpProcessInstanceHandleRelDto handleRelDto, HttpCommandResultDto result) {
        // 封装对象条件
        HttpProcessInstanceHandleRelVo handleRelVo = new HttpProcessInstanceHandleRelVo();
        BeanUtils.copyProperties(handleRelDto, handleRelVo);
        // 查询获取数据列表
        List<FormProcessInstanceHandleRelVo> handleRelVoList = handleRelRepository.selectByCondition(handleRelVo);
        PageInfo<FormProcessInstanceHandleRelVo> pageHandleRels = new PageInfo<>(handleRelVoList);
        result.setPageNum(handleRelVo.getPageNum());
        result.setPageSize(handleRelVo.getPageSize());
        result.setTotalCount((int) pageHandleRels.getTotal());
        // 循环遍历设置返回对象信息
        result.setDatas(handleRelVoList.stream().map(item -> {
            HttpProcessInstanceHandleRelInfoDto tmpDto = new HttpProcessInstanceHandleRelInfoDto();
            BeanUtils.copyProperties(item, tmpDto);
            return tmpDto;
        }).collect(Collectors.toList()));
    }
}
